package org.qydjk.common.elasticsearch;

import com.alibaba.fastjson.JSON;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.qydjk.common.util.es.ESearchUtil;

import java.beans.IntrospectionException;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * ES数据操作基类
 */
public class BaseTransportClientRepository {
    protected Logger log = LogManager.getLogger(getClass());
    protected TransportClient client;

    public BaseTransportClientRepository(TransportClient client) {
        super();
        this.client = client;
    }

    /**
     * 创建文档
     * @param index 索引名称
     * @param type 索引类型
     * @param id 索引id
     * @param doc
     * @return
     */
    public String saveDoc(String index, String type, String id, Object doc) throws InvocationTargetException, IntrospectionException, IllegalAccessException, IOException {
        XContentBuilder builder = ESearchUtil.buildXContentBuilder(doc);
        IndexResponse response = client.prepareIndex(index, type,id).setSource(builder).get();
        return response.getId();
    }

    /**
     * 更新文档
     *
     * @param index
     * @param type
     * @param id
     * @param doc
     * @return
     */
    public String updateDoc(String index, String type, String id, Object doc) throws InvocationTargetException, IntrospectionException, IllegalAccessException, IOException {
        XContentBuilder builder = ESearchUtil.buildXContentBuilder(doc);
        UpdateResponse response = client.prepareUpdate(index, type, id).setDoc(builder).get();
        return response.getId();
    }

    /**
     * 删除文档
     *
     * @param index
     * @param type
     * @param id
     * @return
     */
    public String deleteById(String index, String type, String id) {
        DeleteResponse response = client.prepareDelete(index, type, id).get();
        return response.getId();
    }

    /**
     *  查询文档
     *
     * @param index
     * @param type
     * @param id
     * @return
     */
    public String getIdx(String index, String type, String id) {
        GetResponse response = client.prepareGet(index, type, id).get();
        if (response.isExists()) {
            return response.getSourceAsString();
        }
        return null;
    }

    /**
     * 查询文档并转成对象
     *
     * @param index
     * @param type
     * @param id
     * @param t
     * @return
     */
    public <T> T getIdx(String index, String type, String id, Class<T> t) {
        String res = getIdx(index, type, id);
        return (T) JSON.parseObject(res,t);
    }

    public TransportClient getClient() {
        return client;
    }

    public void setClient(TransportClient client) {
        this.client = client;
    }
}
